package de.uni.bremen.estudent.lbs.views;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import de.uni.bremen.estudent.lbs.util.Constants;
import de.uni.bremen.estudent.lbs.util.LocationAnalyzer;

/**
 * Custom mapview to limit the minimum zoomlevel.
 * 
 * @author bahlfeld
 * @version 1.0 28.02.2012
 */
public final class MyMapView extends MapView {

    /**
     * Constructor of MyMapView.
     * 
     * @param arg0
     *            Context value.
     * @param arg1
     *            AtrributeSet value.
     */
    public MyMapView(final Context arg0, final AttributeSet arg1) {
        super(arg0, arg1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void dispatchDraw(final Canvas canvas) {
        // set max zoom level
        MapController mapController = this.getController();
        if (this.getZoomLevel() <= Constants.ZOOM_LEVEL_15) {
            mapController.setZoom(Constants.ZOOM_LEVEL_16);
            mapController.setCenter(Constants.GEO_POINT_OF_CAMPUS);
            return;
        }
        // limit bounds of map view
        GeoPoint location = this.getMapCenter();
        if (!LocationAnalyzer.isGeoPointInCampus(location)) {
            if (location.getLatitudeE6() < Constants.GEO_POINT_SOUTH_WEST_ON_CAMPUS
                    .getLatitudeE6()) {
                mapController.setCenter(new GeoPoint(
                        Constants.GEO_POINT_SOUTH_WEST_ON_CAMPUS
                                .getLatitudeE6(), location.getLongitudeE6()));
            } else if (location.getLatitudeE6() > Constants.GEO_POINT_NORTH_EAST_ON_CAMPUS
                    .getLatitudeE6()) {
                mapController.setCenter(new GeoPoint(
                        Constants.GEO_POINT_NORTH_EAST_ON_CAMPUS
                                .getLatitudeE6(), location.getLongitudeE6()));
            } else if (location.getLongitudeE6() < Constants.GEO_POINT_SOUTH_WEST_ON_CAMPUS
                    .getLongitudeE6()) {
                mapController.setCenter(new GeoPoint(location.getLatitudeE6(),
                        Constants.GEO_POINT_SOUTH_WEST_ON_CAMPUS
                                .getLongitudeE6()));
            } else if (location.getLongitudeE6() > Constants.GEO_POINT_NORTH_EAST_ON_CAMPUS
                    .getLongitudeE6()) {
                mapController.setCenter(new GeoPoint(location.getLatitudeE6(),
                        Constants.GEO_POINT_NORTH_EAST_ON_CAMPUS
                                .getLongitudeE6()));
            }
            return;
        }

        super.dispatchDraw(canvas);
    }
}
